package com.atguigu.day02;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

class resource{
   private int num=0;

    public int getNum() {
        return num;
    }

    public synchronized void setNum() {
        num++;
    }


    AtomicInteger atomicInteger = new AtomicInteger();

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public void setAtomicInteger() {
        atomicInteger.getAndIncrement();
    }



}




public class CAS {

    public static void main(String[] args) {


//        AtomicInteger atomicInteger = new AtomicInteger(9);
//
//        System.out.println(atomicInteger.compareAndSet(9,9988)+"---"+atomicInteger.get());
//        System.out.println(atomicInteger.compareAndSet(9988,5555)+"----"+atomicInteger.get());



        resource resour = new resource();

//        for (int i = 0; i <200 ; i++) {
//            new Thread(() -> {
//                for (int j = 0; j <200 ; j++) {
//                    resour.setNum();
//                }
//            },"线程").start();
//        }


        for (int i = 0; i <200 ; i++) {
            new Thread(() -> {
                for (int j = 0; j <200 ; j++) {
                    resour.setAtomicInteger();
                }
            },"线程").start();

        }



        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        System.out.println(Thread.currentThread().getName()+"----"+resour.getAtomicInteger());

    }

}
